home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Subversive Component Hacks / DoubleFrames / AdjustDoubleFrames.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  4.7 KB  |  235 lines

  1. #include <Types.h>
  2. #include <Memory.h>
  3. #include <Quickdraw.h>
  4. #include <Fonts.h>
  5. #include <Events.h>
  6. #include <Menus.h>
  7. #include <TextEdit.h>
  8. #include <MacWindows.h>
  9. #include <ControlDefinitions.h>
  10. #include <Dialogs.h>
  11. #include <OSUtils.h>
  12. #include <ToolUtils.h>
  13. #include <Devices.h>
  14. #include <StandardFile.h>
  15. #include <Movies.h>
  16. #include <Sound.h>
  17.  
  18. extern void createWindow(void);
  19. extern void drawWindow( WindowPtr whichWindow );
  20. extern void clickWindow( WindowPtr whichWindow, Point globalWhere );
  21.  
  22. typedef struct {
  23.     Component victim;
  24.     UInt32    extras;
  25. } DoubleFramesSharedGlobalsRecord, **DoubleFramesSharedGlobalsHandle;
  26.  
  27. extern DoubleFramesSharedGlobalsHandle getLCSGHandle(void);
  28. Fixed getExtras(void);
  29. void setExtras( Fixed Extras );
  30.  
  31. Boolean gDone = false;
  32.  
  33. enum {
  34.     kAppleMenuID            = 128,
  35.         kAppleMenuAbout            = 1,
  36.     kFileMenuID                = 129,
  37.         kFileMenuQuit            = 1
  38.     
  39. };
  40.  
  41. static void doMenu( long menuSelection )
  42. {
  43.     short whichMenu = HiWord(menuSelection);
  44.     short whichMenuItem = LoWord(menuSelection);
  45.     
  46.     switch (whichMenu) {
  47.         case kAppleMenuID:
  48.             switch (whichMenuItem) {
  49.                 case kAppleMenuAbout:
  50.                     Alert(128, nil);
  51.                     break;
  52.  
  53.                 default:
  54.                     {
  55.                     Str255 daName;
  56.                     GetMenuItemText(GetMenuHandle(kAppleMenuID), whichMenuItem, daName);
  57.                     OpenDeskAcc(daName);
  58.                     }
  59.                     break;
  60.             }
  61.             break;
  62.  
  63.         case kFileMenuID:
  64.             switch (whichMenuItem) {
  65.                 case kFileMenuQuit:
  66.                     gDone = true;
  67.                     break;
  68.             }
  69.             break;
  70.     }
  71. }
  72.  
  73. int main( void )
  74. {
  75.     GrafPtr wmgrPort;
  76.     
  77.     InitGraf(&qd.thePort);
  78.     InitFonts();
  79.     InitWindows();
  80.     InitMenus();
  81.     TEInit();
  82.     InitDialogs(nil);
  83.     InitCursor();
  84.     
  85.     GetWMgrPort( &wmgrPort );
  86.     SetPort( wmgrPort );
  87.     EnterMovies();
  88.  
  89.     SetMenuBar(GetNewMBar(128));
  90.     AppendResMenu(GetMenuHandle(kAppleMenuID), 'DRVR');
  91.     DrawMenuBar();
  92.  
  93.     if( nil == getLCSGHandle() ) {
  94.         SInt16 itemHit;
  95.         StandardAlert( kAlertStopAlert, "\pI can’t find the DoubleFrames component.  Is it registered?", "\p", nil, &itemHit );
  96.         gDone = true;
  97.     }
  98.     else {
  99.         createWindow();
  100.     }
  101.     
  102.     while (gDone == false) {
  103.         EventRecord theEvent;
  104.         WindowPtr whichWindow;
  105.         short windowPart;
  106.         
  107.         WaitNextEvent(everyEvent, &theEvent, -1, nil);
  108.         
  109.         switch (theEvent.what) {
  110.             case updateEvt:
  111.                 whichWindow = (WindowPtr)theEvent.message;
  112.                 SetPort(whichWindow);
  113.                 BeginUpdate(whichWindow);
  114.                 drawWindow(whichWindow);
  115.                 EndUpdate(whichWindow);
  116.                 break;
  117.             
  118.             case keyDown:
  119.                 if (theEvent.modifiers & cmdKey) {
  120.                     doMenu(MenuKey(theEvent.message & charCodeMask));
  121.                 }
  122.                 break;
  123.             
  124.             case mouseDown:
  125.                 windowPart = FindWindow(theEvent.where, &whichWindow);
  126.  
  127.                 switch (windowPart) {
  128.                     case inDrag:
  129.                         DragWindow(whichWindow, theEvent.where, &qd.screenBits.bounds);
  130.                         break;
  131.  
  132.                     case inGoAway:
  133.                         if (TrackGoAway(whichWindow, theEvent.where))
  134.                             gDone = true;
  135.                         break;
  136.  
  137.                     case inContent:
  138.                         if (whichWindow != FrontWindow())
  139.                         {
  140.                             SelectWindow(whichWindow);
  141.                         }
  142.                         else
  143.                         {
  144.                             clickWindow(whichWindow, theEvent.where);
  145.                         }
  146.                         break;
  147.  
  148.                     case inMenuBar:
  149.                         doMenu(MenuSelect(theEvent.where));
  150.                         break;
  151.                 }
  152.                 break;
  153.         }
  154.     }
  155.     
  156.     return 0;    
  157. }
  158.  
  159. WindowPtr window = nil;
  160. ControlHandle control = nil;
  161.  
  162. void createWindow( void )
  163. {
  164.     Rect windowBounds = { 50, 10, 90, 210 };
  165.     Rect controlBounds = { 10, 10, 30, 190 };
  166.     Fixed value;
  167.     
  168.     window = NewCWindow( nil, &windowBounds, "\pFrame Repetition", true, documentProc, 
  169.             (WindowPtr)-1, true, 0);
  170.     
  171.     SetPort( window );
  172.     control = NewControl( window, &controlBounds, "\p", true, 0, 0, 10, 
  173.             kControlSliderProc + kControlSliderHasTickMarks, 0 );
  174.  
  175.     value = getExtras();
  176.     SetControlValue( control, value );
  177. }
  178.  
  179. void drawWindow( WindowPtr whichWindow )
  180. {
  181.     if( whichWindow == window )
  182.         DrawControls( window );
  183. }
  184.  
  185. void clickWindow( WindowPtr whichWindow, Point globalWhere )
  186. {
  187.     Point localWhere = globalWhere;
  188.     Fixed value;
  189.     
  190.     GlobalToLocal( &localWhere );
  191.      
  192.     if( whichWindow == window ) {
  193.         ControlHandle whichControl = 0;
  194.         ControlPartCode part = FindControl( localWhere, window, &whichControl );
  195.         
  196.         if( whichControl == control ) {
  197.             TrackControl( control, localWhere, NULL );
  198.             
  199.             value = GetControlValue( control );
  200.             setExtras( value );
  201.         }
  202.     }
  203. }
  204.  
  205.  
  206. // returns false if the capture component isn't there.
  207. DoubleFramesSharedGlobalsHandle getLCSGHandle(void)
  208. {
  209.     ComponentDescription cd = { 'rtpr', 'gnrc', 'xtra', 0, 0 };
  210.     Component c;
  211.     
  212.     c = FindNextComponent( 0, &cd );
  213.     if( c )
  214.         return (DoubleFramesSharedGlobalsHandle) GetComponentRefcon( c );
  215.     else
  216.         return 0;
  217. }
  218. Fixed getExtras(void)
  219. {
  220.     DoubleFramesSharedGlobalsHandle sg = getLCSGHandle();
  221.     if( sg && *sg )
  222.         return (*sg)->extras;
  223.     else
  224.         return 0;
  225. }
  226. void setExtras( Fixed extras )
  227. {
  228.     DoubleFramesSharedGlobalsHandle sg = getLCSGHandle();
  229.     if( sg && *sg )
  230.         (*sg)->extras = extras;
  231.     else
  232.         SysBeep(1);
  233. }
  234.  
  235.